<?php
	include_once 'isLogin.php';

	$table_name = "company_product";
	$key_index = array('title','content','upid','state');
	
	$colors = array(
				'state'=>array('0'=>'#ff0000','1'=>'#16d316'),
			);
	$names = array(
				'state'=>array('0'=>'禁用','1'=>'可用'),
			);
	
	//添加
	if($_GET['action']=='add'){
		
		//插入数据
		if(is_array($_POST['values'])){
			foreach($_POST['values'] as $k=>$v){
				$arr[$key_index[$k]] = $v;
			}
		}
		$commonc->insert($table_name,$arr);
		$id = $commonc->insert_id();
		
		//排序
		$arr = array('sort'=>$id);
		$commonc->update($table_name, $arr, "id='".$id."'");
		
		//查询记录
		$data = $commonc->getInfo($table_name, $id);
		
		if($id!=''){
			$return='<tr line_id="'.$id.'">
			<td> <input type="checkbox" name="checkbox" value="'.$id.'" /></td>
			<td value_id="'.$id.'">'.$data['title'].'</td>
			<td value_id="'.$id.'">'.$data['content'].'</td>
			<td>&nbsp;</td>
			<td class="xiugai">
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'up\',\''.$_basename.'?action=sort\')"><img src="images/sort-up.png"/></a>
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'down\','.$_basename.'?action=sort\')"><img src="images/sort-down.png"/></a>
			</td>
			<td class="xiugai"><a href="javascript:;" style="color: '.$colors['state'][$data['state']].'" onclick="state(\''.$id.'\',\'state\',\''.$_basename.'\',$(this))">'.$names['state'][$data['state']].'</a></td>
			<td class="xiugai">
			<a href="javascript:;" onclick="edit(\''.$id.'\',$(this),\''.$_basename.'?action=edit\')">修改</a>';
			if($data['upid']==false) $return.='<a href="'.$_basename.'?id='.$id.'">子类</a>';
			$return.='</td></tr>';
			$arr=array('error'=>'0','content'=>$return);
		}else{
			$arr=array('error'=>'1','content'=>'添加失败');
		}
		echo json_encode($arr);
		exit();
	}
	
	//删除
	if($_GET['action']=='del'){
		$delFile = array(
				array('key'=>'img','dir'=>'upload/image/large/'),
				array('key'=>'img','dir'=>'upload/image/middle/'),
				array('key'=>'img','dir'=>'upload/image/small/'),
				array('key'=>'file','dir'=>'upload/image/file/'),
		);
		
		echo$del_res = $commonc->del($table_name,$_POST['id']);
              //  var_dump($del_res);
		exit();
	}
	
	//修改
	if($_GET['action']=='edit'){
		$arr = array($key_index[$_POST['index']]=>$_POST['value']);
		echo $commonc->update($table_name,$arr,"id='".$_POST['value_id']."'");
		exit();
	}
	
	//修改状态
	if($_GET['action']=='state'){
		echo $commonc->changeState($table_name,$_POST['id'],$_POST['key'],$colors[$_POST['key']],$names[$_POST['key']]);
		exit();
	}
	
	//排序
	if($_GET['action']=='sort'){
		if($_POST['type']=='down') $type = "prev";
		if($_POST['type']=='up') $type = "next";
		$sort = $commonc->sortChange($table_name, $_POST['id'], $type);
		echo $sort['id'];
		exit();
	}
	//搜索
	if($_GET['submit']=='搜索'){
		$where ='';
		if($_GET['company_name']!=''&&$_GET['company_name']!=null)
		{
			$where = " where c.title like '%".$_GET['company_name']."%'";
		}
		
		if($_GET['star']!=''&&$_GET['star']!=null)
		{
			if($where =='')
			{
				$where = " where c.star = '".$_GET['star']."'";
			}
			else
			{
				$where .= " and c.trade_id = '".$_GET['company_trade']."'";
			}
			
		}
		if($_GET['company_level']!=''&&$_GET['company_level']!=null)
		{
			if($where =='')
			{
				$where = " where c.level_id = '".$_GET['company_level']."'";
			}
			else
			{
				$where .= " and c.level_id = '".$_GET['company_level']."'";
			}
			
		}
	}
        if($_GET['id']!=nil&$_GET['id']!=@"")
        {
            $company_id = $_GET['id'];
            $sql = $table_name.' where company_id = "'.$_GET['id'].'"';
	   // $sql.= $where;
	    $sql.= " order by sort desc ";
	    $row=$commonc->getList($sql,$sql);
           
        }
	
	
	
	
	
	//$_SERVER['REQUEST_URI']
        $smarty->assign("company_id", $company_id);
	$smarty->assign("row",$row['result']);
	$smarty->assign('page',$row['page']);
	$smarty->display($_filename.".html");
?>